package ui;

import commonUtil.CommonConst;
import commonUtil.CommonReturn;
import dataValueObject.CardInfoValue;
import dataValueObject.CustomerInfoValue;
import service.CardManager;
import service.MemberManager;

import javax.smartcardio.Card;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static javafx.scene.input.KeyCode.M;
import static service.MemberManager.addMember;
import static service.MemberManager.queryCusotmerInfo;

/**
 * Created by xiezy3 on 2016/8/10.
 */
public class swingUI extends JFrame {
    private JTextField custIdText;
    private JTextField custNameText;
    private JTextField custAddText;
    private JTextField jobText;
    private JTextField useTimesText;
    private JTextField expireDateText;
    // 小容器
    private JLabel commonLabel;
    private JLabel titleLabel;
    private JLabel custIdLabel;
    private JLabel custNameLabel;
    private JLabel custAddLabel;
    private JLabel jobLabel;
    private JLabel useTimesLabel;
    private JLabel expireDateLabel;
    private JLabel cardTypeLabel;

    
    // 小按钮
    private JButton addMemJBu;
    private JButton addCardJBu;
    private JButton queryJBu;

    // 列表框
    private JComboBox cardTypeCB;

    private Font labelFont;
    private Font buttonFont;
    private Font titleFont;
    /*
     * 构造方法
     */
    public swingUI() {
        // 设置窗口标题
        this.setTitle("iyoga 管理系统");
        // 窗体组件初始化
        init();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置布局方式为绝对定位
        this.setLayout(null);

        this.setBounds(0,0,500,700);
        // 窗体大小不能改变
        this.setResizable(false);

        // 居中显示
        this.setLocationRelativeTo(null);

        // 窗体可见
        this.setVisible(true);
    }

    /*
     * 初始化方法
     */
    public void init() {
        // 创建一个容器
        final Container con = this.getContentPane();
        commonLabel = new JLabel();
        commonLabel.setBounds(0,0, 500, 700);
        titleFont = new Font("Dialog",1,25);
        labelFont = new Font("Dialog", 1, 17);
        buttonFont = new Font("Dialog", 1, 15);

        titleLabel = new JLabel("欢迎光临  剑瑜伽瑜伽馆");
        titleLabel.setFont(titleFont);
        titleLabel.setBounds(110,20,300,50);


        custAddLabel = new JLabel("客户地址");
        custAddLabel.setFont(labelFont);
        custAddLabel.setBounds(90,110,90,30);
        custAddText = new JTextField();
        custAddText.setFont(labelFont);
        custAddText.setBounds(210,110,190,30);

        jobLabel = new JLabel("客户职业");
        jobLabel.setFont(labelFont);
        jobLabel.setBounds(90,190,90,30);
        jobText = new JTextField();
        jobText.setFont(labelFont);
        jobText.setBounds(210,190,190,35);

        useTimesLabel = new JLabel("剩余次数");
        useTimesLabel.setFont(labelFont);
        useTimesLabel.setBounds(90,270,90,30);
        useTimesText = new JTextField();
        useTimesText.setFont(labelFont);
        useTimesText.setEnabled(false);
        useTimesText.setBounds(210,270,190,35);

        expireDateLabel = new JLabel("到期时间");
        expireDateLabel.setBounds(90,350,90,30);
        expireDateLabel.setFont(labelFont);

        expireDateText = new JTextField();
        expireDateText.setEnabled(false);
        expireDateText.setFont(labelFont);
        expireDateText.setBounds(210,350,190,35);

        custIdText = new JTextField();
        custIdText.setFont(labelFont);
        custIdText.setBounds(40, 500, 110, 35);
        custIdLabel = new JLabel("客户编号");
        custIdLabel.setFont(labelFont);
        custIdLabel.setBounds(50, 450, 90, 20);


        custNameText = new JTextField();
        custNameText.setFont(labelFont);
        custNameText.setBounds(210, 500, 110, 35);

        custNameLabel = new JLabel("客户姓名");
        custNameLabel.setFont(labelFont);
        custNameLabel.setBounds(220, 450, 90, 20);


        cardTypeLabel = new JLabel("卡片类型");
        cardTypeLabel.setFont(labelFont);
        cardTypeLabel.setBounds(380,450,90,20);

        cardTypeCB = new JComboBox();
        cardTypeCB.addItem("次卡");
        cardTypeCB.addItem("年卡");
        cardTypeCB.setFont(labelFont);
        cardTypeCB.setBounds(380, 500, 70, 35);


        addMemJBu = new JButton("新增客户");
        addMemJBu.setFont(buttonFont);
        addMemJBu.setBounds(50, 580, 100, 40);

        addCardJBu = new JButton("办卡");
        addCardJBu.setFont(buttonFont);
        addCardJBu.setBounds(200, 580, 100, 40);

        queryJBu = new JButton("查询");
        queryJBu.setFont(buttonFont);
        queryJBu.setBounds(350, 580, 100, 40);



        // 所有组件用容器装载
        commonLabel.add(titleLabel);
        commonLabel.add(custIdLabel);
        commonLabel.add(custNameLabel);
        commonLabel.add(custAddLabel);
        commonLabel.add(custAddText);
        commonLabel.add(jobLabel);
        commonLabel.add(jobText);
        commonLabel.add(expireDateLabel);
        commonLabel.add(expireDateText);
        commonLabel.add(useTimesLabel);
        commonLabel.add(useTimesText);
        commonLabel.add(cardTypeCB);
        commonLabel.add(cardTypeLabel);
        commonLabel.add(addMemJBu);
        commonLabel.add(addCardJBu);
        commonLabel.add(queryJBu);
        con.add(commonLabel);
        con.add(custIdText);
        con.add(custNameText);

        //增加客户事件
        addMemJBu.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String custName = custNameText.getText();
                String custAdd = custAddText.getText();
                String job = jobText.getText();
                CustomerInfoValue customerInfoValue = addMemberMethod(custName,custAdd,job);
                custIdText.setText(String.valueOf(customerInfoValue.getCustomerId()));
                CommonReturn commonReturn = customerInfoValue.getCommonReturn();
                JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                        commonReturn.getResultMessage(), "系统信息", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        //查询客户数据事件
        queryJBu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String custIdStr = custIdText.getText();
                String custName = custNameText.getText();
                //将原有数据制空便于展示查询结果
                custAddText.setText(null);
                jobText.setText(null);
                useTimesText.setText(null);
                expireDateText.setText(null);
                int custId = 0;
                if(custIdStr.length() != 0 && custIdStr != null){
                    custId = Integer.valueOf(custIdStr);
                }
                CustomerInfoValue[] customerInfoValues = queryCustomerMethod(custId,custName);
                CommonReturn commonReturn = customerInfoValues[0].getCommonReturn();
                //无效入参或者数据不存在弹窗提示
                if(!commonReturn.getIsSuccess()||CommonConst.ResultCode.NO_DATA_EXIST == commonReturn.getResultCode()){
                    JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                            commonReturn.getResultMessage(), "系统信息", JOptionPane.INFORMATION_MESSAGE);
                }else {
                    //展示客户信息
                    custId = customerInfoValues[0].getCustomerId();
                    custIdText.setText(String.valueOf(custId));
                    custNameText.setText(customerInfoValues[0].getCustomerName());
                    custAddText.setText(customerInfoValues[0].getCustomerAdd());
                    jobText.setText(customerInfoValues[0].getJob());

                    CardInfoValue[] cardInfoValues = queryCardMethod(custId);
                    commonReturn = cardInfoValues[0].getCommonReturn();
                    //展示卡片信息
                    if(CommonConst.ResultCode.SUCCESS == commonReturn.getResultCode()){
                        useTimesText.setText(cardInfoValues[0].getUseTimes());
                        expireDateText.setText(cardInfoValues[0].getExpireDate());
                    }else {
                        //无卡片信息，或者卡片过期提示
                        JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                                commonReturn.getResultMessage(), "系统信息", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            }
        });
        //购卡按钮事件
        addCardJBu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String cardType = String.valueOf(cardTypeCB.getSelectedIndex());
                String custIdStr = custIdText.getText();
                String custName = custNameText.getText();
                CommonReturn commonReturn = new CommonReturn();
                int custId = 0;
                if(custIdStr.length() != 0 && custIdStr != null){
                    custId = Integer.valueOf(custIdStr);
                }
                commonReturn = addCardMethod(cardType,custId,custName);

                JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                        commonReturn.getResultMessage(), "系统信息", JOptionPane.INFORMATION_MESSAGE);

            }
        });



    }
    public static CustomerInfoValue addMemberMethod(String custName, String custAdd, String job){
        CommonReturn commonReturn = new CommonReturn();
        MemberManager memberManager = new MemberManager();
        CustomerInfoValue customerInfoValue = new CustomerInfoValue();
        customerInfoValue.setCustomerName(custName);
        customerInfoValue.setCustomerAdd(custAdd);
        customerInfoValue.setJob(job);
        customerInfoValue.setState(CommonConst.State.VALIDATE);
        customerInfoValue = memberManager.addMember(customerInfoValue);
        return customerInfoValue;

    }

    public static CustomerInfoValue[] queryCustomerMethod(int custId,String custName){
        MemberManager memberManager = new MemberManager();
        CustomerInfoValue[] customerInfoValue = memberManager.queryCusotmerInfo(custId,custName);
        return customerInfoValue;
    }

    public static CardInfoValue[] queryCardMethod(int custId){
        CardManager cardManager = new CardManager();
        CardInfoValue[] cardInfoValues = cardManager.queryCardInfo(custId);
        return cardInfoValues;
    }

    public static CommonReturn addCardMethod(String cardType, int custId, String custName){
        CardManager cardManager = new CardManager();
        CommonReturn commonReturn = cardManager.addCard(cardType, custId, custName);
        return commonReturn;
    }
}
